﻿using GraduationProject.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GraduationProject.Webs.Areas.Admin.Controllers
{
    public class AdminSortListController : Controller
    {
        // GET: Admin/AdminSortList
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 分页获取种类数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetSortListByPage(int page, int limit)
        {
            SortList[] Sort;
            int TotalCount=0;
            using (var conn = new RDBMS.SortList())
            {
                if (conn.TrySelectSortListByPage(out Sort,out TotalCount,page,limit))
                {
                    return new NewtonsoftJson(new { code = 0, msg = "成功", count = TotalCount, data = Sort });
                }
            }
            return Json(new { code = 1, msg = "抱歉,暂无数据", count = 0, data = new List<Novel>() });
        }
        /// <summary>
        /// 后台修改景点操作
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AdminModify(int sortID)
        {
            SortList[] Sort;
            using (var conn = new RDBMS.SortList())
            {
                conn.TrySelectSortList(out Sort, new int[] { sortID });
            }
            Province[] model;
            ScenicLevel[] models;
            //查询各省市区数据信息
            using (var conn = new RDBMS.SortList())
            {
                if (conn.TryGetCiteList(out model, null, null))
                {

                }
            }
            //查询各景区级别数据信息
            using (var conn = new RDBMS.SortList())
            {
                if (conn.TryGetScenicLevelList(out models, null))
                {

                }
            }
            ViewData["Province"] = model;
            ViewData["Level"] = models;
            return View(Sort[0]);
        }
        [HttpPost]
        public ActionResult AdminModify(SortList model)
        {
            var models = new SortList[] { model };
            using (var conn = new RDBMS.SortList())
            {
                if (conn.TryUpdateSortList(out bool result, models))
                {
                    if (result)
                    {
                        return Json(new { code = 0, msg = "修改成功" });
                    }
                }
            }
            return Json(new { code = 1, msg = "修改失败，请稍后重试！" });
        }
        /// <summary>
        /// 后台添加景点操作
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AdminInsertPage()
        {
            Province[] model;
            ScenicLevel[] models;
            //查询各省市区数据信息
            using (var conn = new RDBMS.SortList())
            {
                if (conn.TryGetCiteList(out model, null,null))
                {
                
                }
            }
            //查询各景区级别数据信息
            using (var conn = new RDBMS.SortList())
            {
                if (conn.TryGetScenicLevelList(out models, null))
                {
                    
                }
            }
            ViewData["Province"] = model;
            ViewData["Level"] = models;
            return View();
        }
        [HttpPost]
        public ActionResult AdminInsert(SortList model)
        {
            var models = new SortList[] { model };
            using (var conn = new RDBMS.SortList())
            {
                if (conn.TryInsertSortList(out bool result, models))
                {
                    if (result)
                    {
                        return Json(new { code = 0, msg = "添加成功" });
                    }
                }
            }
            return Json(new { code = 1, msg = "添加失败，请稍后重试！" });
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AdminUploadImage(HttpPostedFileBase file)
        {
            string path;
            if (file != null)
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("~/UploadImg"), Path.GetFileName(file.FileName));
                file.SaveAs(filePath);
                path = "/UploadImg/" + Path.GetFileName(file.FileName);
                return Json(new { code = 0, msg = "成功", data = new { src = path } });
            }
            else
            {
                path = "/Content/layui/images";
                return Json(new { code = 1, msg = "抱歉,上传失败", data = new { src = path } });
            }
        }
        /// <summary>
        /// 景区删除接口
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AdminDelete(int[] ids)
        {
            if (ids != null)
            {
                using (var conn = new RDBMS.SortList())
                {
                    if (conn.TryDeleteSortList(out bool userResult, ids))
                    {
                        if (userResult)
                        {
                            return Json(new { code = 0, msg = "删除成功！" });
                        }
                    }
                }
            }
            return Json(new { code = 1, msg = "删除失败，请稍后重试！" });
        }
    }
}